﻿Imports Microsoft.VisualBasic

Namespace Pic
    Public Class WindowScripts
        Public Shared Sub Alert(ByVal Message As String)
            Dim page As Page = HttpContext.Current.CurrentHandler
            page.RegisterClientScriptBlock("close-popup", _
              "<script language=""JavaScript"">alert(""" & _
                Message & """)</script>")

        End Sub
        Public Shared Function ConfirmAlert(ByVal Message As String) As Boolean
            Dim page As Page = HttpContext.Current.CurrentHandler
            page.RegisterClientScriptBlock("close-popup", _
              "<script language=""JavaScript"">if(confirm(""" & Message & """)==true) return true; else return false;</script>")
        End Function
        Public Shared Sub CloseWindow(Optional ByVal refreshParent As Boolean = False)
            Dim page As Page = HttpContext.Current.CurrentHandler
            page.RegisterClientScriptBlock("close-popup", _
              "<script language=""JavaScript"">" & _
               GetCloseWindowScript(refreshParent) & "</script>")
        End Sub

        Public Shared Function GetCloseWindowScript(Optional _
          ByVal refreshParent As Boolean = False) As String
            Dim script As String = ""
            If refreshParent Then
                script = "var refreshHelper = window.opener.document.getElementById('btnRefresh');if (refreshHelper !=null) {refreshHelper.click();}"
            End If
            Return script & "self.close();"
        End Function

        Public Shared Function GetOpenWindowScript(ByVal URL As String, ByVal winName As String, ByVal Height As Integer, ByVal Width As Integer, ByVal StatusBar As Boolean, _
                ByVal Resizable As Boolean, ByVal ScrollBars As Boolean, ByVal MenuBar As Boolean, ByVal Toolbar As Boolean, ByVal Location As Boolean, ByVal CopyHistory As Boolean) As String
            Dim Name As String = "null"
            If Not winName Is Nothing Then Name = "'" & winName & "'"
            Dim features As String = String.Format("height={0}, width={1}, status={2}, resi" _
               & "zable={3}, scrollbars={4}, menubar={5}, toolbar={6}, location={7}, copyhi" _
               & "story={8}", _
                Height, Width, YesNo(StatusBar), YesNo(Resizable), YesNo( _
                   ScrollBars), YesNo(MenuBar), YesNo(Toolbar), YesNo(Location), _
                   YesNo(CopyHistory))
            Dim ScrptString As String
            ScrptString = String.Format("var LeftPosition = (screen.width) ? (screen.width-" & Width & ")/2 : 0;var TopPosition = (screen.height) ? (screen.height-" & Height & ")/2 : 0; var popWin = window.open('{0}', {1}, '{2},Top='+TopPosition+',Left='+LeftPosition);", URL, Name, features)
            ScrptString &= ";if (!popWin) {alert('Popup sayfa açılamadı. Bilgisayarınızda popup önleyici yazılım yüklenmişse, programı kullanabilmek için popup sayfaların açılmasına izin vermelisiniz.');} else {;}"
            Return ScrptString


            'ScrptString &= "; "
        End Function

        Private Shared Function YesNo(ByVal val As Boolean) As String
            If val Then Return "yes" Else Return "no"
        End Function
    End Class
End Namespace